package fr.insalyon.sims.broadcast.tests;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import fr.insalyon.sims.broadcast.network.*;
import fr.insalyon.sims.broadcast.service.BroadcastTransmitterService;

public class TransmitterServiceTest {

	/**
	 * @param args
	 * @throws UnknownHostException 
	 * @throws SocketException 
	 */
	public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
		int listeningPort = 3432;
		int multicastPort = 22593;
		String multicastIp = "224.0.0.1";
		InetAddress group = InetAddress.getByName(multicastIp);
		
		// Create multicast receiver stream
		MCastStreamReceiver receiver = new MCastStreamReceiver(group,multicastPort);
		receiver.addDataListener(new DataListener() {
			public void dataReceived(DataEvent event) {
				String data = new String(event.getData(), event.getOffset(), event.getLength());
				System.out.println("[receiver] data received: " + data);
			}
		});
		receiver.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace();
			}
		});
		
		receiver.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace();
			}
		});

		// Start the stream
		receiver.connect();
		receiver.start();
		System.out.println("[receiver] started");
		
		// Service parameters
		HashMap<String, String> params = new HashMap<String, String>();
		
		// Create and start the transmitter service
		BroadcastTransmitterService service = new BroadcastTransmitterService(listeningPort, group, multicastPort);
		service.doStart(params);
		
		// Create TCP transmitter which is the data source
		TCPStreamTransmitter transmitter = new TCPStreamTransmitter(InetAddress.getLocalHost(), listeningPort);
		
		// Start the stream
		transmitter.connect();
		transmitter.start();
		
		for (int i = 0; i < 10; i++) {
			String data = i + "hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !";
			transmitter.send(data.getBytes(), 0, data.length());
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		
		// Stop all streams
		transmitter.stop();
		transmitter.disconnect();
		service.doStop();
		receiver.stop();
		receiver.disconnect();

	}
	// ------------------------------------------------------- CONSTANTS -- //
	// ------------------------------------------------------ ATTRIBUTES -- //
	// ---------------------------------------------------- CONSTRUCTORS -- //
	// --------------------------------------------------------- METHODS -- //
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
